﻿using System;

namespace homework3
{
    public abstract class Shape 
    {
        abstract  public double getArea ();
        abstract public double getCircumference();
        abstract public bool ifLegle();

    }
    class Rectangle :Shape
    {
        double length;
        double width;
        public Rectangle () 
        {
            Random rd1 = new Random();
            Random rd2 = new Random();

            length = rd1.NextDouble()*10 ;
            width = rd2.NextDouble() * 10;
        }
        override public double getArea() 
        {
            double area=0.0;
            area = length * width;
            Console.WriteLine("长方形的面积是" + area);
            return area;
        }
        override public double getCircumference() 
        {
            double circumference = 0.0;
            circumference = 2 * (length + width);
            Console.WriteLine("长方形的周长是" + circumference);
            return circumference;
        }
        override public bool ifLegle()
        {
            bool legle = true;
            if (length <= 0 || width <= 0)
            {
                legle = false;
                Console.WriteLine("输入数据有误，不构成长方形");
            }
            else
            {
                Console.WriteLine("输入数据正确，构成长方形");
            }
                return legle;
            
        }

            


    }
    class Square : Shape
    {
        double length;
       
        public Square()
        {
            Random rd3 = new Random();
            length = rd3.NextDouble()*10;
          
        }
        override public double getArea()
        {
            double area = 0.0;
            area = length * length;
            Console.WriteLine("正方形的面积是" + area);
            return area;
        }
        override public double getCircumference()
        {
            double circumference = 0.0;
            circumference = 4 * length;
            Console.WriteLine("正方形的周长是" + circumference);
            return circumference;
        }
        override public bool ifLegle()
        {
            bool legle = true;
            if (length <= 0 )
            {
                legle = false;
                Console.WriteLine("输入数据有误，不构成正方形");
            }
            else { Console.WriteLine("输入数据正确，构成正方形"); }
            
            return legle;
        }




    }
    class Triangle : Shape
    {
        double length1;
        double length2;
        double length3;

        public Triangle()
        {
            Random rd4 = new Random();
            double[] suiji = new double[3];
           for(int i = 0; i < 3; i++) 
            {
                suiji[i] = rd4.NextDouble()*10;

            }
            length1 = suiji[0];
            length2 = suiji[1];
            length3 = suiji[2];

        }
        override public double getArea()
        {
            double area = 0.0;
            double p = (length1 + length2 + length3) / 2;
            area = System.Math.Sqrt(p*(p - length1)*(p - length2)*(p - length3));
            Console.WriteLine("三角形的面积是" + area);
            return area;
        }
        override public double getCircumference()
        {
            double circumference = 0.0;
            circumference = length1 + length2 + length3;
            Console.WriteLine("三角形形的周长是" + circumference);
            return circumference;
        }
        override public bool ifLegle()
        {
            bool legle = true;
            if (length1 <= 0||length2<=0||length3<=0)
            {
                legle = false;
            }
            if (length1 + length2 <= length3 || length1 + length3 <= length2 || length2 + length3 <= length1)
            {
                legle = false;
              
            }
            
            return legle;
        }




    }
    class Program
    {

        static void Main(string[] args)
        { Shape[] shape = new Shape[10];
            double sum=0; 
            for (int i = 0; i <= 9; i++)
            {

                Random rd = new Random();
                int temp = rd.Next(1, 4);
                
                switch (temp)
                {
                    case 1:
                        Rectangle rectangle = new Rectangle();
                        shape[i] = rectangle;
                        sum += rectangle.getArea();
                        break;
                    case 2:
                        Square square = new Square();
                        shape[i] = square;
                        sum += square.getArea();
                        break;
                    case 3:
                        Triangle triangle = new Triangle();
                        while (!triangle.ifLegle())
                        {
                             triangle = new Triangle();
                        }
                        shape[i] = triangle;
                        sum += triangle.getArea();
                        break;

                }
            }

            Console.WriteLine("十个图形已生成");
            
            Console.WriteLine("十个图形的面积和为" + sum);


        }
     

          
          
            

        
    }
}

